from app.extensions import db


class GarbageKnowledge(db.Model):
    __tablename__ = 'garbage_knowledge'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), unique=True, nullable=False, comment='垃圾名称')
    category = db.Column(db.String(32), nullable=False, comment='垃圾类别（0: 可回收，1: 有害，2: 厨余，3: 其他）')
    tip = db.Column(db.String(256), nullable=False, comment='垃圾投放说明')

    def to_dict(self):
        return {
            'name': self.name,
            'lajitype': self.category,
            'lajitip': self.tip
        }

    @staticmethod
    def get_by_name(name):
        return GarbageKnowledge.query.filter_by(name=name).first()

    @staticmethod
    def search_by_keyword(keyword):
        return GarbageKnowledge.query.filter(GarbageKnowledge.name.like(f'%{keyword}%')).all()

    @staticmethod
    def get_by_category(category_code):
        return GarbageKnowledge.query.filter_by(category=str(category_code)).all()
